<?php
namespace app\admin\controller;
use think\Validate;
use think\Loader;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;//首先引入 Request 文件；

//use app\admin\controller\Base;
use app\admin\model\Examination_records as Examination_recordsModel; //改名，否则会有两个Admin类，报错
session_start();
header('Content-Type:application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");//允许跨域请求
class Exam extends Controller{
	/*public function _initialize(){ //在类里执行任何方法之前都要执行这个方法
      if(!session('username')){
      	$this->error('非法进入，请先登录！','Login/index');
      }
	}*/

	public function index(){ //在线考试
        //点击开始考试生成一条考试记录
        $cid=$_POST['session_cid'];
        $data['cid']=$cid;
        $data['stime']=time();//开始时间
        db('examination_records')->insert($data);

    		$list_1=Db::query("select exid,etitle,etype,score,a,b,c,d from tp_test_questions where etype='1' order by rand() limit 5 ");          //单选
    		$list_2=Db::query("select exid,etitle,etype,score,a,b,c,d from tp_test_questions where etype='2' order by rand() limit 5 ");          //多选
    		$num_1=count($list_1);
    		$num_2=count($list_2);

        $this->assign('list_1',$list_1);
        $this->assign('list_2',$list_2);
        $this->assign('num_1',$num_1);
        $this->assign('num_2',$num_2);
    		$questions=[
    		 'dan'=>$list_1,
    		 'dan_num'=>$num_1,
    		 'duo'=>$list_2,
    		 'duo_num'=>$num_2,
    		];
    		//return $this->fetch('index1');
    		return 	json_encode($questions,JSON_UNESCAPED_UNICODE);
	}
	public function sub(){
        if(request()->isPost()){
            $arr = request()->param();
            $cid=$_POST['session_cid'];
            $rid = Db::name('examination_records')->where('cid',$cid)->where('etime','')->value('rid');//查询到考试记录号
            $stime = Db::name('examination_records')->where('cid',$cid)->where('etime','')->value('stime');//查询开始时间

            $num=0;
            $defeng=0;

            if(isset($arr['answer1'])){
                $danxuan = $arr['answer1'];
                foreach($danxuan as $key => $val){
                   $answer1 = Db::name('test_questions')->where("exid = '$key'")->value('answer');
                   if ($val == $answer1) {
                       $num=$num+10;
                       $defeng=10;
                   }else{
                    $defeng=0;
                   }
                   //插入记录到考试明细表
                   $mingxi_1=[
                   'rid'=>$rid,
                   'exid'=>$key,
                   'answer'=>$answer1,
                   'eanswer'=>$val,
                   'score'=>$defeng,
                    ];
                   db('examination_details')->insert($mingxi_1);
                }
            }
            if(isset($arr['answer2'])){
                $duoxuan = $arr['answer2'];
                foreach($duoxuan as $key => $val){
                   $answer2 = Db::name('test_questions')->where("exid = '$key'")->value('answer');
                   $dx_str = implode('', $val);
                   if($dx_str == $answer2){
                       $num=$num+10;
                       $defeng=10;
                   }else{
                       $defeng=0;
                   }
                   //插入记录到考试明细表
                   $mingxi_2=[
                   'rid'=>$rid,
                   'exid'=>$key,
                   'answer'=>$answer2,
                   'eanswer'=>$dx_str,
                   'score'=>$defeng,
                    ];
                   db('examination_details')->insert($mingxi_2);
                }
            }

            //补全考试记录表
            $data['etime']=time();//结束时间
            $data['ltime']=time()-$stime;//所用时间
            $data['score']=$num;
            Db::query("update tp_examination_records set etime = '".$data['etime']."', ltime = '".$data['ltime']."',score='".$data['score']."' where cid = '".$cid."' and etime='' ");

            //判断是否几个及返回数据
            if($num>=60){
             $update=Db::query("update tp_broker set exam = '1' where cid='".$cid."'");
             $exam_info['pass']='1';//及格
             $exam_info['score']=$num;
             $exam_info['use_time']=time()-$stime;
             return json_encode($exam_info,JSON_UNESCAPED_UNICODE);
            }else{
             $exam_info['pass']='2';//不及格
             $exam_info['score']=$num;
             $exam_info['use_time']=time()-$stime;
             return json_encode($exam_info,JSON_UNESCAPED_UNICODE);
            }
        }//计算考试成绩 //计算考试成绩
    }
	public function lst(){ //查看考试记录
      $cid=$_POST['session_cid'];
      $list = Db::name('examination_records')->where('cid',$cid)->select();//查询考试记录
			$lst_info[]=$list;
			return json_encode($lst_info,JSON_UNESCAPED_UNICODE);
	}
	public function apply_put(){//提交执业认证
		if(request()->isPost()){
      $cid=$_POST['session_cid'];
                $data=[
                  'cid'=>$cid,
                  'ftime'=>time(),
                ];

            //上传图片
            if(isset($_FILES['fidcard']['tmp_name'])){
                $file=request()->file('fidcard');
                $info = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif,jpeg'])->move( 'apply' . DS . 'renzheng');//5M
                $fidcard['fidcard']='/public/apply/renzheng/'.$info->getSaveName();
                if(db('qualification')->where('cid',$cid)->setField('fidcard',$fidcard['fidcard'])){
                  $apply_put=[
                  'message'=>'yes',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
                }else{
                  $apply_put=[
                  'message'=>'no',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
                }
            }
            if(isset($_FILES['fphoto']['tmp_name'])){//2
                $file=request()->file('fphoto');
                $info = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif,jpeg'])->move( 'apply' . DS .  'renzheng');//5M
                $fphoto['fphoto']='/public/apply/renzheng/'.$info->getSaveName();
                if(db('qualification')->where('cid',$cid)->setField('fphoto',$fphoto['fphoto'])){
                  $apply_put=[
                  'message'=>'yes',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
              }else{
                  $apply_put=[
                  'message'=>'no',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
                }
            }
            if(isset($_FILES['fedu']['tmp_name'])){  //1
                $file=request()->file('fedu');
                $info = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif,jpeg'])->move( 'apply' . DS . 'renzheng');//5M
                $data['fedu']='/public/apply/renzheng/'.$info->getSaveName();
                if(db('qualification')->insert($data)){
                  Db::query("update tp_broker set registry = '3' where cid = '".$cid."' ");//修改认证状态为待审核
                  $apply_put=[
                  'message'=>'yes',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
                }else{
                  $apply_put=[
                  'message'=>'no',
                  ];
                return json_encode($apply_put,JSON_UNESCAPED_UNICODE);
                }
            }
           }
	}

}








